#include "Camera.h"

D3DXVECTOR3 CCamera::s_WorldUp(0, 1, 0);

CCamera::CCamera(void)
{
	D3DXMatrixIdentity(&m_Projection);
	D3DXMatrixIdentity(&m_View);

	m_fWidth = 1024.0f;
	m_fHeight = 768.0f;
	
	m_fNear = 0.1f;
	m_fFar = 300.0f;
	m_fFieldofView = 45.0f;
}

CCamera::~CCamera(void)
{
}

const D3DXMATRIX CCamera::GetView()
{ 
	D3DXMATRIX mRot(m_View);
	mRot._41 = 0;
	mRot._42 = 0;
	mRot._43 = 0;
	D3DXMatrixTranspose(&mRot,&mRot);
	
	D3DXMATRIX mTrans;
	D3DXMatrixIdentity(&mTrans);
	mTrans._41 = m_View._41;
	mTrans._42 = m_View._42;
	mTrans._43 = m_View._43;

	D3DXMatrixMultiply(&mTrans,&mTrans,&mRot);
	
	//mTrans *= -1.0f;
	//mTrans._11 *= -1.0f;
	//mTrans._12 *= -1.0f;
	//mTrans._13 *= -1.0f;
	mTrans._31 *= -1.0f;
	mTrans._32 *= -1.0f;
	mTrans._33 *= -1.0f;


	return (mTrans);

}

void CCamera::Initialize(float fWidth, float fHeight)
{
	m_fWidth = fWidth;
	m_fHeight = fHeight;
	BuildPerspective();
}

void CCamera::BuildPerspective()
{
	D3DXMatrixPerspectiveFovLH(&m_Projection, D3DXToRadian(m_fFieldofView), m_fWidth/m_fHeight, m_fNear, m_fFar);
}



D3DXMATRIX CCamera::GetViewProjection(void)
{
	D3DXMATRIX temp;
	D3DXMatrixIdentity(&temp);
	D3DXMatrixMultiply(&temp,&m_View,&m_Projection);
	return temp;
}

void CCamera::SetProjectionMatrix(D3DXMATRIX *_Matrix)
{
	memcpy(&m_Projection, _Matrix, sizeof(D3DXMATRIX));
}

void CCamera::SetViewMatrix(D3DXMATRIX *_Matrix)
{
	memcpy(&m_View, _Matrix, sizeof(D3DXMATRIX));

	m_View._11 = -m_View._11;
	m_View._12 = -m_View._12;
	m_View._13 = -m_View._13;
}

D3DXVECTOR3 CCamera::GetViewXAxis(void)
{
    return D3DXVECTOR3(m_View._11, m_View._12, m_View._13);
}

void CCamera::SetViewXAxis(D3DXVECTOR3 _vPosition)
{
    m_View._11 = _vPosition.x;
    m_View._12 = _vPosition.y;
    m_View._13 = _vPosition.z;
}
D3DXVECTOR3 CCamera::GetViewYAxis(void)
{
    return D3DXVECTOR3(m_View._21, m_View._22, m_View._23);
}

void CCamera::SetViewYAxis(D3DXVECTOR3 _vPosition)
{
    m_View._21 = _vPosition.x;
    m_View._22 = _vPosition.y;
    m_View._23 = _vPosition.z;
}
D3DXVECTOR3 CCamera::GetViewZAxis(void)
{
    return D3DXVECTOR3(m_View._31, m_View._32, m_View._33);
}

void CCamera::SetViewZAxis(D3DXVECTOR3 _vPosition)
{
    m_View._31 = _vPosition.x;
    m_View._32 = _vPosition.y;
    m_View._33 = _vPosition.z;
}
D3DXVECTOR3 CCamera::GetViewPositionAxis(void)
{
    return D3DXVECTOR3(m_View._41, m_View._42, m_View._43);
}

void CCamera::SetViewPositionAxis(D3DXVECTOR3 _vPosition)
{
    m_View._41 = _vPosition.x;
    m_View._42 = _vPosition.y;
    m_View._43 = _vPosition.z;
}


/*====================================================================/
    Local Transform Functions
//===================================================================*/
void CCamera::ViewRotateLocalX(float _fAngle)
{
    D3DXMATRIX  _mRotation;
	D3DXVECTOR3 _vLocalX(m_View._11, m_View._12, m_View._13);

    D3DXMatrixRotationAxis(&_mRotation, &_vLocalX, _fAngle);

    D3DXVECTOR4 Position(m_View._41, m_View._42,m_View._43, m_View._44);

    m_View._41 = m_View._42 = m_View._43 = 0.0f; 
    
    D3DXMatrixMultiply(&m_View, &m_View, &_mRotation);
    
	m_View._41 = Position.x; 
	m_View._42 = Position.y;
    m_View._43 = Position.z; 
	m_View._44 = Position.w;
}

void CCamera::ViewRotateLocalY(float _fAngle)
{
    D3DXMATRIX _mRotation;
	D3DXMatrixIdentity(&_mRotation);
    D3DXVECTOR4 Position(m_View._41, m_View._42,m_View._43, m_View._44);
    D3DXMatrixRotationY(&_mRotation, FLOAT(_fAngle));
    
    m_View._41 = m_View._42 = m_View._43 = 0.0f;
	
    D3DXMatrixMultiply(&m_View, &m_View, &_mRotation);
    m_View._41 = Position.x; 
	m_View._42 = Position.y;
    m_View._43 = Position.z;
	m_View._44 = Position.w;

}

void CCamera::ViewRotateLocalZ(float _fAngle)
{
    D3DXMATRIX  _mRotation;
    D3DXVECTOR3 _vLocalZ(m_View._31, m_View._32, m_View._33);
    D3DXMatrixRotationAxis(&_mRotation, &_vLocalZ, _fAngle);
    
    D3DXVECTOR4 Position(m_View._41, m_View._42,m_View._43, m_View._44);
    m_View._41 = m_View._42 = m_View._43 = 0.0f; 
    
    D3DXMatrixMultiply(&m_View, &m_View, &_mRotation);

    m_View._41 = Position.x; 
	m_View._42 = Position.y;
    m_View._43 = Position.z; 
	m_View._44 = Position.w;
}

void CCamera::ViewTranslateLocal(D3DXVECTOR3 _vAxis)
{
    
    ViewTranslateLocalX(_vAxis.x);
    ViewTranslateLocalY(_vAxis.y);
    ViewTranslateLocalZ(_vAxis.z);
}

void CCamera::ViewTranslateLocalX(float _fScale)
{
    m_View._41 += (m_View._11 * _fScale);
    m_View._42 += (m_View._12 * _fScale);
    m_View._43 += (m_View._13 * _fScale);
}

void CCamera::ViewTranslateLocalY(float _fScale)
{

    m_View._41 += (m_View._21 * _fScale);
    m_View._42 += (m_View._22 * _fScale);
    m_View._43 += (m_View._23 * _fScale);
}

void CCamera::ViewTranslateLocalZ(float _fScale)
{
    m_View._41 += (m_View._31 * _fScale);
    m_View._42 += (m_View._32 * _fScale);
    m_View._43 += (m_View._33 * _fScale);
}

/*====================================================================/
    Global Transform Functions
//===================================================================*/
void CCamera::ViewRotateGlobalX(float _fAngle)
{
    D3DXMATRIX _mRotation;
    D3DXVECTOR4 Position(m_View._41, m_View._42, m_View._43, m_View._44);

    D3DXMatrixRotationX(&_mRotation, _fAngle);
    
    m_View._41 = m_View._42 = m_View._43 = 0.0f; 
    
    D3DXMatrixMultiply(&m_View, &m_View, &_mRotation);

    m_View._41 = Position.x; 
	m_View._42 = Position.y;
    m_View._43 = Position.z; 
	m_View._44 = Position.w;
}

void CCamera::ViewRotateGlobalY(float _fAngle)
{
    D3DXMATRIX _mRotation;
    D3DXVECTOR4 Position(m_View._41, m_View._42,m_View._43, m_View._44);
    D3DXMatrixRotationY(&_mRotation, _fAngle);
    
    m_View._41 = m_View._42 = m_View._43 = 0.0f; 
    
    D3DXMatrixMultiply(&m_View, &m_View, &_mRotation);
    m_View._41 = Position.x; 
	m_View._42 = Position.y;
    m_View._43 = Position.z; 
	m_View._44 = Position.w;
}

void CCamera::ViewRotateGlobalZ(float _fAngle)
{
    D3DXMATRIX _mRotation;
    D3DXVECTOR4 Position(m_View._41, m_View._42,m_View._43, m_View._44);
    D3DXMatrixRotationZ(&_mRotation, _fAngle);
    
    m_View._41 = m_View._42 = m_View._43 = 0.0f; 
    
    D3DXMatrixMultiply(&m_View, &m_View, &_mRotation);
    m_View._41 = Position.x; 
	m_View._42 = Position.y;
    m_View._43 = Position.z; 
	m_View._44 = Position.w;
}

void CCamera::ViewTranslateGlobal(D3DXVECTOR3 _vAxis)
{
    m_View._41 += _vAxis.x;
    m_View._42 += _vAxis.y;
    m_View._43 += _vAxis.z;
}

void CCamera::ViewTranslateGlobalX(float _fScale)
{
    m_View._41 += _fScale;
}

void CCamera::ViewTranslateGlobalY(float _fScale)
{
    m_View._42 += _fScale;
}

void CCamera::ViewTranslateGlobalZ(float _fScale)
{
    m_View._43 += _fScale;
}



